package cn.vcjf.washcar.washcarorder.service.impl;

import cn.vcjf.washcar.common.util.DateUtils;
import cn.vcjf.washcar.common.util.excel.ExcelExportUtil;
import cn.vcjf.washcar.server.entity.ServerInfo;
import cn.vcjf.washcar.server.mapper.ServerInfoMapper;
import cn.vcjf.washcar.washcarorder.entity.WashCarOrder;
import cn.vcjf.washcar.washcarorder.mapper.WashCarOrderMapper;
import cn.vcjf.washcar.washcarorder.service.WashCarOrderService;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.*;

/**
 * <p>
 * 洗车订单表 服务实现类
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-17
 */
@Service("washCarOrderService")
public class WashCarOrderServiceImpl extends ServiceImpl<WashCarOrderMapper, WashCarOrder> implements WashCarOrderService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private WashCarOrderMapper washCarOrderMapper;
	@Resource
	private ServerInfoMapper serverInfoMapper;

	@Override
	public Map<String, Double> findWashCarOrderStatistics() {
		return baseMapper.findWashCarOrderStatistics();
	}

	@Override
	public Map<String,Object> getManagerIndexData() {
		Map<String,Object> mainIndexOrderCount=baseMapper.findMainIndexOrderCount();
		Map<String,Object> orderTransaction=baseMapper.findOrderTransaction();
		Map<String,Object> userCount=baseMapper.findUserCount();
		Map<String, Object> orderCount = baseMapper.findOrderCount();

		Map<String, Object> data = new HashMap<>();
		data.put("mainIndexOrderCount", mainIndexOrderCount);
		data.put("orderTransaction", orderTransaction);
		data.put("userCount", userCount);
		data.put("orderCount", orderCount);
		return data;
	}

	@Override
	public List<Map<String, Object>> getManagerIndexOrderData(Date begDate, Date endDate) {
		return baseMapper.getManagerIndexOrderData(begDate,endDate);
	}

	@Override
	public List<Map<String, Object>> getDayOfMonthOrderCount() {
		return baseMapper.getDayOfMonthOrderCount();
	}

	@Override
	public List<Map<String, Object>> getNear7DaysOrderCount() {
		return baseMapper.getNear7DaysOrderCount();
	}

	@Override
	public void exportWashCarOrder(WashCarOrder washCarOrder, HttpServletResponse response) {
// 输出Excel文件
		OutputStream output = null;
		try {
			Page<WashCarOrder> page = washCarOrder.getPageInfo();

			List<WashCarOrder> washCarOrderList = washCarOrderMapper.selectPageList(page, washCarOrder);
			// 定义表的标题
			String title = "订单数据";

			//定义表的列名
			String[] rowsName = new String[]{
					"会员信息", "订单编号","订单金额", "优惠金额","订单状态", "服务类型", "服务状态"
					, "洗车时间", "洗车地点", "车辆信息","下单时间","支付时间"};
			//定义表的内容
			List<Object[]> dataList = new ArrayList<Object[]>();
			for (int i = 0; i < washCarOrderList.size(); i++) {
				WashCarOrder entity = washCarOrderList.get(i);
				Object[] objs = new Object[rowsName.length];
				String payStatus = " ";
				String userInfo= entity.getUserName()+"("+entity.getUserPhone()+")";
				objs[0] = userInfo;
				objs[1] = entity.getOrderNo() == null ? " " : entity.getOrderNo();

				int status=entity.getStatus();
				//	1下单中,2下单成功,3下单失败,4支付成功,5支付失败,6已撤销
				switch (status){
					case 1:payStatus = "下单中";break;
					case 2:payStatus = "下单成功";break;
					case 3:payStatus = "下单失败";break;
					case 4:payStatus = "支付成功";break;
					case 5:payStatus = "支付失败";break;
					case 6:payStatus = "已取消";break;
				}
				objs[2] = entity.getMoney();
				objs[3] = entity.getDiscounts()+" ";

				objs[4] = payStatus;

				ServerInfo serverInfo = serverInfoMapper.selectById(entity.getServerType());
				objs[5] = serverInfo.getServerName() + "";

				String orderServiceStatus = " ";
				int serviceStatus = entity.getServiceStatus();
				switch (serviceStatus){
					case 1:orderServiceStatus="未服务";break;
					case 2:orderServiceStatus="服务中";break;
					case 3:orderServiceStatus="已服务";break;
				}
				objs[6] = orderServiceStatus;

				objs[7] = DateUtils.formatDate(entity.getWashStartTime(), "yyyy-MM-dd HH:mm:ss")
						+ " - " + DateUtils.formatDate(entity.getWashEndTime() , "yyyy-MM-dd HH:mm:ss");
				objs[8] = entity.getWashAddress()+" ";
				objs[9] = entity.getCarInfo();
				objs[10] = entity.getCreateTime()!=null?DateUtils.formatDate(entity.getCreateTime(), "yyyy-MM-dd HH:mm:ss"):"";
				objs[11] = entity.getPayTime()!=null?DateUtils.formatDate(entity.getPayTime(), "yyyy-MM-dd HH:mm:ss"):"";

				dataList.add(objs);
			}

			// 创建ExportExcel对象
			ExcelExportUtil ex = new ExcelExportUtil(title, rowsName, dataList);

			// 输出Excel文件
			output = response.getOutputStream();
			response.reset();
			String fileName = "订单数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xls";
			response.setHeader("Content-disposition", "attachment; filename=" + new String(fileName.getBytes(), "ISO-8859-1"));
			response.setContentType("application/msexcel");
			ex.export(output);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("订单数据导出异常！", e);
		} finally {
			try {
				if (output != null) {
					output.close();
				}
			} catch (Exception e) {

			}
		}
	}

	@Override
	public List<WashCarOrder> selectPageList(RowBounds rowBounds, WashCarOrder washCarOrder) {
		return baseMapper.selectPageList(rowBounds,washCarOrder);
	}
}
